package io.renren.modules.weixin.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.weixin.Service.WeixinService;
import io.renren.modules.weixin.Service.WxBindService;
import io.renren.modules.weixin.Service.ZbAdminService;
import io.renren.modules.weixin.Service.ZbResultService;
import io.renren.modules.weixin.entity.WxBindEntity;
import io.renren.modules.weixin.entity.ZbAdminEntity;
import io.renren.modules.weixin.entity.ZbResultEntity;
import io.renren.modules.weixin.message.senderror.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: Mr-wang
 * @date: 2018/4/19 0019 21:27
 * @Description:
 */
@RestController
@RequestMapping("/wx/zb")
public class ZbController extends AbstractController{

    @Autowired
    private ZbResultService zbResultService;
    @Autowired
    private ZbAdminService zbAdminService;
    @Autowired
    private WxBindService wxBindService;
    @Autowired
    private WeixinService weixinService;

    @GetMapping("/zbresultlist")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = zbResultService.queryPage(params);
        return R.ok().put("page",page);
    }

    @GetMapping("/zbconfiglist")
    public R clist(@RequestParam Map<String, Object> params){

        PageUtils page = zbAdminService.queryPage(params);
        return R.ok().put("page",page);
    }

    @GetMapping("/selectzb")
    public List<ZbResultEntity> selectzb(@RequestParam Map<String, Object> params){
      return zbResultService.selectzb();
    }

    @PostMapping("/zbconfigadd")
    public R zbconfigadd(@RequestBody ZbAdminEntity zbparams){
          zbAdminService.configzb(zbparams);
        return R.ok();
    }

    /**
     * 删除zb
     *
     */
    @PostMapping("/zbconfigdelete")
    public R delete(@RequestBody ZbAdminEntity zbAdminEntity){
        zbAdminService.deleteByzbCodeAndcreator(zbAdminEntity.getZbCode(),zbAdminEntity.getCreator());
        return R.ok();
    }

    /**
     *根据 zbCode and creator查询
     */
    @PostMapping("zbconfigselect")
    public ZbAdminEntity cselect(@RequestBody ZbAdminEntity zbAdminEntity){
        return zbAdminService.selectByzbCodeAndcreator(zbAdminEntity.getZbCode(),zbAdminEntity.getCreator());
    }

    /**
     *更新zb
     */
    @PostMapping("/zbconfigupdate")
    public R update(@RequestBody ZbAdminEntity zbAdminEntity){
        zbAdminService.updateByzbCodeAndcreator(zbAdminEntity);
        return R.ok();
    }

    /**
     * 模拟生成指标
     */
    @PostMapping("/addtest")
    public R addtest(@RequestBody ZbResultEntity zbR){
        Date opt = new Date();
        zbR.setOpTime(opt);
        if("norule" == zbR.getDqState()){
            zbResultService.insert(zbR);
        } else {
            weixinService.senderror(opt,zbR);
        }
        return R.ok();
    }

    /**
     * 配置指标 状态更改
     */
    @PostMapping("/statusopen")
    public R Sopen(@RequestBody ZbAdminEntity zbA){
        zbAdminService.updateStatusOpen(zbA.getZbCode(),zbA.getCreator());
        return R.ok();
    }

    /**
     * 配置指标 状态关闭
     */
    @PostMapping("/statusclose")
    public  R Sclose(@RequestBody ZbAdminEntity zbA){
        zbAdminService.updateStatusClose(zbA.getZbCode(),zbA.getCreator());
        return R.ok();
    }
}

